package com.ai.utils;


import com.ai.constant.SystemConst;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * DES ECB对称加密 解密
 */
public class DesECBUtil {



    /**
     * 加密数据
     * @param str
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(String str, String key)
            throws Exception {

        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] byteMi = cipher.doFinal(str.getBytes());
        return byteMi;

    }

    /**
     * 解密数据
     * @param bytes
     * @param key
     * @return
     * @throws Exception
     */
    public static String decrypt(byte[] bytes, String key)
            throws Exception {

        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] byteMi = cipher.doFinal(bytes);
        return new String(byteMi);

    }

    /**
     * 测试方法
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String clearText = "zhouxiuhong";
        String key = UUID.randomUUID().toString().replaceAll("-", "");
        
         key = "166377c38df04afcad3e85eea45e535f";
//
//        //加密
        clearText = com.ai.utils.Base64Util.encode(encrypt(clearText, key));
         System.out.println(clearText);

        String aa = DesECBUtil.decrypt(com.ai.utils.Base64Util.decode(URLDecoder.decode("PBP3skpujDk%3D", "UTF-8")), SystemConst.TOKEN_VALIDATE_KEY);
        String bb = com.ai.utils.Base64Util.encode(DesECBUtil.encrypt("admin", SystemConst.TOKEN_VALIDATE_KEY));

        System.out.println("aa ==="+aa);
        System.out.println("bb ==="+ URLEncoder.encode(bb, "utf-8"));


//		String sign="7wnGpZhWTSo3V3wYFJ1PokE+epHqetLPqX4LR9lHuDon94DmiJgBoPbx7HnPcTkmp8+kayXY1ufIFGm0Ulrna4HP+2gkzFISrzeqepp/q4XvWDEu+P/Xb1mV9mbgmHyJNk/mMGo9+6cWPc0bv0GgwBZtPrAEXSW07IOV/7bM8fVdgvZtwvJebpSfFsrROjhsirX3ZEjdghCNzG9MKsqGiPm5y2V3fljl7qye0EYneSBaO8tv1z0YcDujU03wtHAxMUnF8ROAtwSVmGSQa6IdqMTazTtYPdogkIqEUptfbCDIjYKZNSO16CJT4S66hRH3w5VXaJzbbLasJwHJwBx8t5Ke5WDfqenPnLrJZH3SChrjJHoXWNL/8GmY1540ypl4JTb0Vqa2hRBIgFp7kyxZfgf62WpNXOFJccJIhiwLcehCvK/quNV8IPQUJXvGy2p2EGKlL7TJjE30dG7iB4fo76kA0UwuSpCyqqJFy7NOtle7xG92BEZUMUHege/Sj7jj3uN9EsRhJ/i6N1o9yxW8iw==";

        //解密
//		String decryptText = decrypt(Base64Util.decode(clearText),key);
//		String decryptText = decrypt(Base64Util.decode(clearText),key);
//        System.out.println(decryptText);

        //密钥
        /*String sign = "166377c38df04afcad3e85eea45e535f";
        String mw = "CRMadmin";
        mw = Base64Util.encode(encrypt(mw, sign));
        System.out.println("加密："+mw);*/
    }
}
